package com.neuedu.filter;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@WebFilter(filterName = "CrosFilter",urlPatterns = "")
public class CrosFilter implements Filter {
    @Override
    public void destroy() {
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse respose, FilterChain chain) throws ServletException, IOException {
        //注意：这里设置只允许http://localhost:8081进行跨域访问
        HttpServletResponse resp= (HttpServletResponse) respose;
        resp.setHeader("Access-Control-Allow-Origin", "http://localhost:8848");
        resp.setHeader("Access-Control-Allow-Credentials", "true");
        resp.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE, PUT");
        resp.setHeader("Access-Control-Max-Age", "3628800");
        resp.setHeader("Access-Control-Allow-Headers", "x-requested-with,Authorization");
        chain.doFilter(req, resp);
    }

    @Override
    public void init(FilterConfig config) throws ServletException {

    }

}
